#include "stdafx.h"
#include "Marco.h"

char* DoubleToChar(double value, int numAfterPoint, char *out)
{
	char * buf = 0;
	int decimal;
	int sign;
	int err;
	buf = (char*)malloc(_CVTBUFSIZE);
	err = _fcvt_s(buf, _CVTBUFSIZE, value, numAfterPoint, &decimal, &sign);
	char result[100];
	memset(result, 0, 100);
	if (!err)
	{
		int n = strlen(buf);

		if (decimal > 0)
		{
			int t = 0;
			for (int i = 0; i < decimal; i++)
			{
				result[t++] = buf[i];
			}
			result[t++] = '.';
			int count = 0;
			for (int i = decimal + 1; count<n + 1; i++, count++)
			{
				result[t++] = buf[i - 1];
			}
			//return result;
		}
		else
		{
			result[0] = '0';
			result[1] = '.';
			int t = 2;
			for (int i = 0; i < -decimal; i++)
				result[t++] = '0';
			for (int i = 0; i<n + 1; i++)
				result[t++] = buf[i];

			//return result;
		}
	}
	free(buf);
	strcpy_s(out, strlen(result) + 1, result);
	return result;
}